<template>
  <div class="login-container">
    <div class="login-card">
      <div class="login-title">重置密码</div>
      <div class="login-subtitle">请设置您的新密码</div>
      
      <el-form :model="verificationData" :rules="rules" ref="formRef" label-width="0">
        <el-form-item prop="newPassword">
          <el-input
            v-model="verificationData.newPassword"
            type="password"
            placeholder="请输入新密码"
            size="large"
            :prefix-icon="Lock"
            show-password
            clearable
            :disabled="loading"
          />
        </el-form-item>
        <el-form-item prop="confirmPassword">
          <el-input
            v-model="verificationData.confirmPassword"
            type="password"
            placeholder="请确认新密码"
            size="large"
            :prefix-icon="Lock"
            show-password
            clearable
            :disabled="loading"
          />
        </el-form-item>
        <el-form-item>
          <el-button class="login-button" type="primary" @click="onSubmit" :loading="loading">
            {{ loading ? '重置中...' : '重置密码' }}
          </el-button>
        </el-form-item>
      </el-form>
      
      <div class="login-footer">
        已想起密码？<el-link type="primary" @click="goToLogin" :disabled="loading">返回登录</el-link>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { Lock } from '@element-plus/icons-vue'
import { resetPassword } from '@/api/auth'
import { userStorage } from '@/utils/storage'

const router = useRouter()
const formRef = ref()
const loading = ref(false)
const verificationData = ref({
  email:'',
  phone:'',
  newPassword:'',
  confirmPassword:''
})
const form = ref({
  password: '',
  confirmPassword: '',
})

// 密码验证规则
const validatePassword = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请输入密码'))
  } else if (value.length < 6) {
    callback(new Error('密码长度不能少于6位'))
  } else if (!/^(?=.*[a-zA-Z])(?=.*\d)/.test(value)) {
    callback(new Error('密码必须包含字母和数字'))
  } else {
    callback()
  }
}

// 确认密码验证规则
const validateConfirmPassword = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请确认密码'))
  } else if (value !== verificationData.value.newPassword) {
    callback(new Error('两次输入的密码不一致'))
  } else {
    callback()
  }
}

const rules = {
  password: [
    { validator: validatePassword, trigger: 'blur' }
  ],
  confirmPassword: [
    { validator: validateConfirmPassword, trigger: 'blur' }
  ],
}

// 检查验证状态
onMounted(() => {
  // 从存储中获取验证信息
  // const storedVerification = userStorage.getPasswordResetVerification()

  // console.log('Stored verification data:', storedVerification)

  let storageUser=JSON.parse(localStorage.getItem("storageUser"))

  
  // 检查是否有验证信息（允许未验证的数据）
  if (!storageUser) {
    ElMessage.error('请先输入邮箱和手机号')
    router.push('/forgot-password')
    return
  }
  
  // 检查必要参数
  if (!storageUser.email || !storageUser.phone) {
    ElMessage.error('验证信息不完整，请重新输入')
    userStorage.removePasswordResetVerification()
    router.push('/forgot-password')
    return
  }
  
  // 存储验证数据供后续使用
  verificationData.value.email=storageUser.email
  verificationData.value.phone=storageUser.phone


  
})

async function onSubmit() {

  console.log(verificationData.value);
  

  if (!formRef.value) return
  
  try {
    // 前端表单验证
    await formRef.value.validate()
    
    var res=await resetPassword(verificationData.value);
    console.log(res);
    if(res.code===200){
      ElMessage.success(`${res.message}`)
      router.push('/login')
    }else{
      ElMessage.error(`${res.message}`)
    }
    

    // 设置加载状态
    loading.value = true
    
    // 检查验证数据是否存在
    if (!verificationData.value) {
      ElMessage.error('验证信息丢失，请重新输入邮箱和手机号')
      router.push('/forgot-password')
      return
    }
    

    

    
  
    
 
    
  } catch (error) {
    console.error('Reset password error:', error)
  

  } finally {
    // 重置加载状态
    loading.value = false
  }
}

function goToLogin() {
  if (!loading.value) {
    router.push('/login')
  }
}
</script>

<style scoped>
.login-container {
  height: 100vh;
  width: 100vw;
  display: flex;
  align-items: center;
  justify-content: center;
  background-image: url('@/assets/images/login-bg.svg');
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  padding: 20px;
  position: relative;
  overflow: hidden;
  box-sizing: border-box;
}
.login-container::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(2px);
  z-index: 1;
  animation: backgroundPulse 8s ease-in-out infinite;
}
@keyframes backgroundPulse {
  0%, 100% { opacity: 0.1; }
  50% { opacity: 0.15; }
}
.login-card {
  position: relative;
  z-index: 2;
  background: rgba(255, 255, 255, 0.6);
  backdrop-filter: blur(16px) saturate(180%);
  -webkit-backdrop-filter: blur(16px) saturate(180%);
  border: 1px solid rgba(255, 255, 255, 0.3);
  border-radius: 16px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  padding: 40px;
  width: 100%;
  max-width: 400px;
  max-height: 90vh;
  overflow-y: auto;
}
.login-title {
  font-size: 28px;
  font-weight: 600;
  color: #2c3e50;
  margin: 0 0 8px 0;
  text-align: center;
  animation: titleGlow 4s ease-in-out infinite;
}
@keyframes titleGlow {
  0%, 100% { text-shadow: 0 0 8px rgba(102, 126, 234, 0.4); }
  50% { text-shadow: 0 0 25px rgba(102, 126, 234, 0.8); }
}
.login-subtitle {
  color: #7f8c8d;
  font-size: 14px;
  margin: 0 0 30px 0;
  text-align: center;
}
.el-form {
  margin-bottom: 20px;
}
.el-form-item {
  margin-bottom: 20px !important;
}
.el-form :deep(.el-input__wrapper) {
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(5px);
  border: 1px solid rgba(255, 255, 255, 0.3);
  transition: all 0.3s ease;
}
.el-form :deep(.el-input__wrapper:hover) {
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  border-color: rgba(102, 126, 234, 0.3);
}
.el-form :deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 0 0 2px rgba(102, 126, 234, 0.2);
  border-color: #667eea;
}
.login-button {
  width: 100%;
  height: 48px;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 500;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
}
.login-button::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
  transition: left 0.5s;
}
.login-button:hover::before {
  left: 100%;
}
.login-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 16px rgba(102, 126, 234, 0.3);
}
.login-button:active {
  transform: translateY(0);
}
.login-footer {
  text-align: center;
  color: #7f8c8d;
  font-size: 14px;
  margin-top: 18px;
}
.login-footer .el-link {
  font-size: 14px;
}
@media (max-width: 480px) {
  .login-card {
    padding: 30px 20px;
    margin: 10px;
  }
  .login-title {
    font-size: 24px;
  }
}
</style> 